/**
 * 
 */
package com.itmayiedu.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.util.StringUtils;

import com.itmayiedu.annotation.IsMobile;
import com.itmayiedu.utils.ValidatorUtil;

/**
 * @author Administrator
 * @date 2019年5月2日
 */
public class IsMobileValidator implements ConstraintValidator<IsMobile, String> {

	private boolean require = false;
	
	/* (non-Javadoc)
	 * @see javax.validation.ConstraintValidator#initialize(java.lang.annotation.Annotation)
	 */
	@Override
	public void initialize(IsMobile constraintAnnotation) {
		System.out.println("IsMobileValidator...");
		this.require = constraintAnnotation.required();
	}
	
	@Override
	/* (non-Javadoc)
	 * @see javax.validation.ConstraintValidator#isValid(java.lang.Object, javax.validation.ConstraintValidatorContext)
	 */
	
	public boolean isValid(String arg0, ConstraintValidatorContext arg1) {
		boolean flag = false;
		if(require){
			flag = ValidatorUtil.isMobile(arg0);
		}else {
			if(!StringUtils.isEmpty(arg0)){
				flag = ValidatorUtil.isMobile(arg0);
			}
		}
		
		return flag;
		
	}

}
